import 'reflect-metadata';
type MyPropDecorator = (targetClassPrototype: any,propertyKey: string | symbol) => void;

/**
 *autowire依赖注入的意思
 * @param dependencyid 唯一标识传参，
 * @param singleton 是否是单件设计模式，单例设计模式
 * @returns
 */
export  function Autowired( dependencyid?: string,singleton?: boolean): MyPropDecorator {
  return (targetClassPrototype, propertyKey) => {
    let ServiceImplInstance:any
    // 修饰的是 ../service/UserSerice.ts
    // targetClassPrototype 就是 UserController 中的 原型对象属性集合
    // propertyKey 就是  private userServiceImpl!: UserServiceInter;中的userServiceImpl 修饰的定义的属性变量
    let ServiceImplInstanceOrClass = Reflect.getMetadata( 'ServiceImplInstanceOrClass', targetClassPrototype, propertyKey);
    let metaSingleton = Reflect.getMetadata("singleton",targetClassPrototype,propertyKey)

    if (metaSingleton) {
      // 是单例模式
      ServiceImplInstance = ServiceImplInstanceOrClass
    } else {
      ServiceImplInstance = new ServiceImplInstanceOrClass();
    }
    Reflect.defineProperty(targetClassPrototype, propertyKey, {
      value: ServiceImplInstance,
    });
  };
}
